var express = require('express');
const axios = require('axios');

var router = express.Router();

router.get('/', function (req, res, next) {
    // res.send("个人信息页面");
    res.render('app/post');
})
var id

/**
 * @api {get} /post/post/:id/detail 渲染帖子
 * @apiName 帖子
 * @apiGroup Post
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get("/post/:id/detail", function (req, res, next) {
    id = req.params.id;
    console.log(id)
    // console.log(req.session.user.id)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-post/getPostData',
        params: {
            postId: id,
            userId: req.session.user.id
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 'success') {
                console.log("成功")
                var data = response.data.data
                console.log(data)
                res.render('app/post', {
                    list: data
                });
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取数据失败" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "获取数据失败" })
            console.log("失败")
        });
})

/**
 * @api {get} /post/getComment 获取评论信息
 * @apiName 帖子
 * @apiGroup Post
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/getComment', function (req, res, next) {
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-comment-top/findCTByPost',
        params: {
            PostId: id
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response);
            if (response.data.code == 'success') {
                console.log("获取评论成功")
                var data = response.data.data
                console.log(data)
                res.send({ err: 0, msg: "获取评论成功",data: data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取评论失败" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("axios请求失败")
        });
})

/**
 * @api {post} /post/getTwoComment 获取二级评论信息
 * @apiName 帖子
 * @apiGroup Post
 *
 * @apiParam {String} post 评论id.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/getTwoComment', function (req, res, next) {
    let {commentId} = req.body
    console.log(commentId)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-comment-two/findAllCommentTwoByTop',
        params: {
            TopId: commentId
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response);
            if (response.data.code == 'success') {
                console.log("获取二级评论成功")
                var data = response.data.data
                res.send({ err: 0, msg: "获取二级评论成功",data: data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取二级评论失败" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("axios请求失败")
        });
})

/**
 * @api {post} /post/sendMyComment 评论帖子
 * @apiName 帖子
 * @apiGroup Post
 *
 * @apiParam {String} post 帖子id.
 * @apiParam {String} commentData 评论内容.
 * 
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/sendMyComment', function (req, res, next) {
    let {commentData} = req.body
    console.log(commentData)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-comment-top/addCommentTop',
        params: {
            post: id,
            commentData: commentData
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response);
            if (response.data.code == 'success') {
                console.log("获取评论成功")
                res.redirect('/post/post/'+ id +'/detail');
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "评论失败，请重试" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("axios请求失败")
        });
})

/**
 * @api {post} /post/sendTwoComment 发送二级评论
 * @apiName 帖子
 * @apiGroup Post
 *
 * @apiParam {String} CommentId 被评论ID.
 * @apiParam {String} commentData 评论内容.
 * 
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/sendTwoComment', function (req, res, next) {
    let {CommentId,commentData} = req.body
    console.log({CommentId,commentData})
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-comment-two/addCommentTwo',
        params: {
            CommentId: CommentId,
            commentData: commentData
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response);
            if (response.data.code == 'success') {
                console.log("发送二级评论成功")
                res.redirect('/post/post/'+ id +'/detail');
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "发送二级评论失败，请重试" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("axios请求失败")
        });
})

module.exports = router;